package com.jilian.mydemo.modul.exam.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.PartnerDescriptionDto;
import com.jilian.mydemo.modul.user.activity.IsReviewingActivity;
import com.jilian.mydemo.modul.user.activity.ReviewFailuerActivity;
import com.jilian.mydemo.modul.user.activity.ReviewSuccessActivity;
import com.jilian.mydemo.modul.user.viewmodel.UserViewModel;
import com.jilian.mydemo.utils.ToastUitl;

/**
 * 合作说明
 */
public class CooperationThatActivity extends BaseActivity {
    private TextView tvOk;

    private UserViewModel viewModel;
    private WebView webview;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }


    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_cooperationthat;
    }

    @Override
    public void initView() {
        setNormalTitle("合作说明", v -> finish());
        tvOk = (TextView) findViewById(R.id.tv_ok);
        webview = (WebView) findViewById(R.id.webview);

    }
    /**
     * 加载html标签
     *
     * @param bodyHTML
     * @return
     */
    private String getHtmlData(String bodyHTML) {
        String head = "<head>" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> " +
                "<style>img{max-width: 100%; width:auto; height:auto!important;}</style>" +
                "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }

    @Override
    public void initData() {
        showLoadingDialog();
        viewModel.getPartnerDescription();
        viewModel.getPartnerDescriptionliveData().observe(this, new Observer<BaseDto<PartnerDescriptionDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<PartnerDescriptionDto> dto) {
                hideLoadingDialog();
                    if(dto.isSuccess()&&dto.getData()!=null){
                        //content是后台返回的h5标签
                        webview.loadDataWithBaseURL(null,
                                getHtmlData(dto.getData().getPartnerDescription()), "text/html", "utf-8", null);
                    }
                    else{
                        ToastUitl.showImageToastTips(dto.getMsg());
                    }
            }
        });
    }

    @Override
    public void initListener() {
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              //获取审核人状态 ，根据审核人状态跳转
                showLoadingDialog();
                viewModel.getPartnerApply();
                viewModel.getApplyStatusliveData().observe(CooperationThatActivity.this, new Observer<BaseDto<PartnerDescriptionDto>>() {
                    @Override
                    public void onChanged(@Nullable BaseDto<PartnerDescriptionDto> dto) {
                        hideLoadingDialog();
                        if(dto.isSuccess()){
                            if(dto.getData()!=null){

                                if(dto.getData().getApplyStatus()==0){
                                    //审核中
                                    Intent intent = new Intent(CooperationThatActivity.this, IsReviewingActivity.class);
                                    intent.putExtra("title", "合伙人申请");
                                    startActivity(intent);
                                }
                                if(dto.getData().getApplyStatus()==1){
                                    Intent intent = new Intent(CooperationThatActivity.this, ReviewSuccessActivity.class);
                                    intent.putExtra("title", "合伙人申请");
                                    startActivity(intent);
                                }
                                if(dto.getData().getApplyStatus()==2){
                                    Intent intent = new Intent(CooperationThatActivity.this, ReviewFailuerActivity.class);
                                    intent.putExtra("title", "合伙人申请");
                                    startActivity(intent);

                                }
                                if(dto.getData().getApplyStatus()==3){
                                    startActivity(new Intent(CooperationThatActivity.this,PartnershipApplicationActivity.class));
                                }

                            }
                            else{
                                ToastUitl.showImageToastTips(dto.getMsg());
                            }


                        }
                        else{
                         ToastUitl.showImageToastTips(dto.getMsg());
                        }


                    }
                });



            }
        });
    }
}
